#include "Spectrum.h"

#include <QtCore/QFile>
#include <QtCore/QTextStream>

namespace spectClean {

Spectrum::Spectrum() : _startFreq(0.0), _freqInc(0.0)
{
}


Spectrum::~Spectrum()
{
}


void Spectrum::clear()
{
    _freq.clear();
    _amp.clear();
}

void Spectrum::resize(const unsigned n)
{
    _freq.resize(n, 0.0);
    _amp.resize(n, Complex(0.0, 0.0));
}

void Spectrum::save(const QString & filename) const
{
    QFile file(filename);
    if (QFile::exists(filename))
        QFile::remove(filename);

    if (!file.open(QIODevice::WriteOnly))
        return;

    QTextStream out(&file);
    out.setNumberFlags(QTextStream::ForceSign);
    out.setRealNumberNotation(QTextStream::FixedNotation);
    out.setRealNumberPrecision(8);
    out.setFieldWidth(4);
    for (unsigned i = 0; i < _freq.size(); ++i)
    {
        out << i << " ";
        out << _freq[i] << " ";
        out << _amp[i].real() << " ";
        out << _amp[i].imag() << " ";
        out << std::abs(_amp[i]) << " ";
        out << endl;
    }

    file.close();
}


} // namespace spectClean
